package com.example.cet4try.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.cet4try.R;
import com.example.cet4try.Utils.MyAsyncTask;
import com.example.cet4try.myDialog.LoadDialog;
import com.example.cet4try.Utils.MySQLUtils;

public class ActivityRegist extends AppCompatActivity {

    private EditText userame,name,pwd,pwd2;
    private Button btn_regist;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        init();

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setBG();
            }
        };
        userame.addTextChangedListener(textWatcher);
        name.addTextChangedListener(textWatcher);
        pwd.addTextChangedListener(textWatcher);
        pwd2.addTextChangedListener(textWatcher);

        btn_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String spwd = pwd.getText().toString();
                String spwd2 = pwd2.getText().toString();
                if (!spwd.equals(spwd2)){
                    Toast.makeText(ActivityRegist.this,"两次输入的密码不一致！",Toast.LENGTH_SHORT).show();
                    return;
                }
                regist();

            }
        });

    }

    private void init(){
        userame = findViewById(R.id.username);
        name = findViewById(R.id.name);
        pwd = findViewById(R.id.pwd);
        pwd2 = findViewById(R.id.pwd2);
        btn_regist = findViewById(R.id.btn_regist);
        preferences = getSharedPreferences("App_Shared_Data", 0);
    }

    private void setBG(){
        if (checkText()){
            btn_regist.setEnabled(true);
            btn_regist.setBackground(getResources().getDrawable(R.drawable.button_login2));
        }
        else{
            btn_regist.setEnabled(false);
            btn_regist.setBackground(getResources().getDrawable(R.drawable.button_login1));
        }

    }

    private boolean checkText(){
        if (userame.getText().toString().length() < 1){
            name.setHint("昵称");
            return false;
        }
        if (name.getText().toString().length() < 1){
            name.setHint(userame.getText().toString());
        }
        if (pwd.getText().toString().length() < 1)
            return false;
        if (pwd2.getText().toString().length() < 1)
            return false;

        return true;
    }

    private void regist(){
        MyAsyncTask task = new MyAsyncTask(ActivityRegist.this,"正在注册");
        task.setOnExecute(new MyAsyncTask.OnExecute() {
            private String spwd,suser,snick;
            private boolean flag = false;
            @Override
            public void before() {
                spwd = pwd.getText().toString();
                suser = userame.getText().toString();
                snick = name.getText().toString();
                if (TextUtils.isEmpty(snick))
                    snick = suser;
            }
            @Override
            public void main() {
                if (!MySQLUtils.CheckUser(suser))
                    flag = MySQLUtils.AddUser(suser,snick,spwd);
            }
            @Override
            public void after() {
                if (flag){
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString("nickname",snick);
                    editor.putString("username",suser);
                    editor.apply();
                    startActivity(new Intent(getApplicationContext(),Main2Activity.class));
                    finish();
                }
                else
                    Toast.makeText(ActivityRegist.this,"用户名已存在，注册失败",Toast.LENGTH_SHORT).show();
            }
        });
        task.execute();
    }

}
